﻿using Models;
using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommTestNFW.ImageCode
{
    /// <summary>
    /// 二维码标签
    /// </summary>
    public class CodeLabel
    {
        public CodeLabel(PointF point, float width, float height, float pixel)
        {
            Width = width;
            Height = height;

            Size = new SizeF(width, height);
            Point = point;

            Padding padding = LabelConfig.RealLabelPadding;
            Padding = padding.Boost(pixel);
        }

        //七周样本 黑色(默认色)
        public Brush Brush { get; set; } = Brushes.Black;

        public float Width { get; set; }
        public float Height { get; set; }
        public SizeF Size { get; private set; }
        /// <summary>
        /// 计算padding后的大小
        /// </summary>
        public SizeF InnerSize { get; private set; }
        private Padding padding;
        public Padding Padding
        {
            get { return padding; }
            set
            {
                //需要设置像素, 放大
                padding = value;

                InnerPoint = new PointF(Point.X + padding.Left, Point.Y + padding.Top);
                InnerSize = new SizeF(Size.Width - padding.Horizontal, Size.Height - padding.Vertical);
                QrCode = new QrCode(InnerSize.Width);
            }
        }

        /// <summary>
        /// 左上角起始点
        /// </summary>
        public PointF Point { get; set; }
        /// <summary>
        /// 计算padding后的左上角起始点
        /// </summary>
        public PointF InnerPoint { get; set; }

        private PrintLabNo printLabNo;
        public PrintLabNo PrintLabNo
        {
            get { return printLabNo; }
            set
            {
                printLabNo = value;
                Code = printLabNo.LabNo;

                if (printLabNo.SpecimenType == "P")
                {
                    //重做样本  深绿色
                    Brush = Brushes.DarkGreen;
                }
                else if (printLabNo.Week == 6)
                {
                    //6周样本  红色
                    Brush = Brushes.Red;
                }

            }
        }

        public QrCode QrCode { get; set; }
        private string code;
        public string Code
        {
            get { return code; }
            set
            {
                code = value;
                SplitCode(value);
            }
        }

        public LineText Line1Text { get; set; }
        public LineText Line2Text { get; set; }

        public static int RowMaxLen = 5;

        //ZM20  12A0001
        //ZM20  12A60001
        //ZM20  P12A0001
        //ZM20  P12A60001
        public void SplitCode(string code)
        {
            if (code == null || code.Length == 0)
                return;

            //单行最大数字
            var rowMaxLen = RowMaxLen;
            string text1, text2; int len1, len2;
            if (code.Length < 10)
            {
                var len = code.Length;
                len1 = len / 2;
                len2 = len - len1;
                text1 = code.Substring(0, len1);
                text2 = code.Substring(len1);
            }
            else
            {
                var showText = code.Substring(4);
                var len = showText.Length;
                if (len < 5) return;

                //倒数第五位数字
                var r5 = showText.Substring(len - 5, 1);//60001
                len2 = r5 == "6" ? 5 : 4; //第二行的长度

                len1 = len - len2; //第一行的长度 2-4位  4A - P12J
                text1 = showText.Substring(0, len1);//P12J
                text2 = showText.Substring(len1);//60001  或者 0001
            }
            Line1Text = new LineText() { Length = len1, Text = text1 };
            Line2Text = new LineText() { Length = len2, Text = text2 };
        }
    }


    /// <summary>
    /// 行级文字
    /// </summary>
    public class LineText
    {
        public int Length { get; set; }
        public string Text { get; set; }

        //public Color Color { get; set; }
    }

    /// <summary>
    /// 正方形二维码
    /// 宽高相等
    /// </summary>
    public class QrCode
    {
        public QrCode(float width)
        {
            Width = width;
            Height = width;
        }

        public QrCode(float width, float height) : this(width)
        {
            Height = height;
        }

        public float Width { get; set; }
        public float Height { get; set; }

        //public Color Color { get; set; }
    }
}
